from app import db
import os


# 不在数据库内使用外键约束进行数据库更新操作
# 而是使用当前常用的软连接，软删除设立isDelete字段进行软删除操作
# 以此提升数据库性能和安全性

class User(db.Model):
    __tablename__ = 'users'
    userid = db.Column(db.Integer(), autoincrement=True, primary_key=True, unique=True)
    account = db.Column(db.String(11), unique=True)
    nickname = db.Column(db.String(30), nullable=False)
    password_hash = db.Column(db.String(32), nullable=False)
    head_pic = db.Column(db.String(30), nullable=False, default='1.jpg')
    isAdmin = db.Column(db.Boolean, default=False)
    isDelete = db.Column(db.Boolean, default=False)
    createTime = db.Column(db.DateTime)
    updateTime = db.Column(db.DateTime)


class Follow(db.Model):
    __tablename__ = 'follow'
    followerid = db.Column(db.Integer(), nullable=False, primary_key=True)
    fanid = db.Column(db.Integer(), nullable=False, primary_key=True)
    isDelete = db.Column(db.Boolean, default=False)
    createTime = db.Column(db.DateTime)
    updateTime = db.Column(db.DateTime)


class Essay(db.Model):
    __tablename__ = 'essay'
    essayid = db.Column(db.Integer(), autoincrement=True, primary_key=True, unique=True)
    authorid = db.Column(db.Integer(), nullable=False)
    isDelete = db.Column(db.Boolean, default=False)
    createTime = db.Column(db.DateTime)
    updateTime = db.Column(db.DateTime)
    support_num = db.Column(db.Integer(), default=0)


    def get_essay_url(self):
        BASE_DIR = os.path.dirname(os.path.abspath(__file__))
        essay_path = os.path.join(BASE_DIR, 'essay')
        essay_path = os.path.join(essay_path, str(self.essayid))
        url = essay_path
        return url

    def delete_essay(self, now_time):
        self.isDelete = True
        supports = Support.query.join(Essay,Support.essayid==self.essayid).filter(Support.isDelete==False).all()
        for support in supports:
            support.isDelete = True
            support.updateTime = now_time

    def get_support(self, user_id, now_time):

        support = Support.query.filter_by(userid=user_id,essayid=self.essayid).first()
        if support is None:
            support = Support(userid=user_id,essayid=self.essayid,createTime=now_time, updateTime=now_time)
            db.session.add_all([support])
            self.support_num += 1
        else:
            if support.isDelete == False:
                return
            support.isDelete = False
            support.updateTime = now_time
            self.support_num += 1


    def not_support(self, user_id, now_time):

        support = Support.query.filter_by(userid=user_id,essayid=self.essayid).first()
        if support is not None:
            if support.isDelete == False:
                support.isDelete = True
                support.updateTime = now_time
                self.support_num -= 1



class Support(db.Model):
    __tablename__ = 'support'
    supportid = db.Column(db.Integer(), autoincrement=True, primary_key=True, unique=True)
    userid = db.Column(db.Integer(), nullable=False)
    essayid = db.Column(db.Integer(), nullable=False)
    isDelete = db.Column(db.Boolean, default=False)
    createTime = db.Column(db.DateTime)
    updateTime = db.Column(db.DateTime)